package com.gitee.wsl.android.logger

import android.util.Log
import com.gitee.wsl.logger.LogLevel
import com.gitee.wsl.logger.LogPrinter

/**
 * Android日志记录器
 */
class AndroidLogPrinter() : LogPrinter {
    override fun log(
        level: LogLevel,
        tag: String,
        messageAny: Any?,
        throwable: Throwable?
    ) {
        val message = messageAny.toString()
        val androidLogLevel = level.ordinal + 2

        val messageWithError = if (throwable != null) {
            message + "\n" + Log.getStackTraceString(throwable)
        } else message

        Log.println(androidLogLevel, tag, messageWithError)
    }

    companion object{
        val instant = AndroidLogPrinter()
    }

}

private val isAndroidPlatform by lazy {
    runCatching {
        Class.forName("android.util.Log", false, AndroidLogPrinter::class.java.classLoader)
    }.isSuccess
}

val LogPrinter.Companion.Android get() = AndroidLogPrinter.instant